using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;

namespace CEQuery
{
    public partial class ChangePasswordDB : Form
    {
        public ChangePasswordDB()
        {
            InitializeComponent();
            NewConnectionString = string.Empty;
            OldConnectionString = string.Empty;
        }

        public string OldConnectionString { get; private set; }
        public string NewConnectionString { get; private set; }

        private bool ValidateInput()
        {
            bool isValid = true;
            if (this.txtFile.Text.Trim() == string.Empty)
            {
                isValid = false;
                errValidation.SetError(btnSelect, "Select a file.");
            }
            else
            {
                errValidation.SetError(btnSelect, string.Empty);
            }
            if (this.txtPassword.Text == txtNewPassword.Text)
            {
                isValid = false;
                errValidation.SetError(txtNewPassword, "Old & New password must be different.");
            }
            else
            {
                errValidation.SetError(txtNewPassword, string.Empty);
            }
            return isValid;
        }

        
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!ValidateInput())
            {
                return;
            }
            try
            {
                //string conn = string.Format("Data Source={0};Password={1};Max Database Size=4091;Max Buffer Size = 1024;Default Lock Escalation =100;Encrypt Database=TRUE", txtFile.Text, txtPassword.Text);
                string conn = string.Format("Data Source={0};Password={1};Max Database Size=4091;Max Buffer Size = 1024;Default Lock Escalation =100;Encrypt Database={2}", txtFile.Text, txtPassword.Text, txtPassword.Text.Length == 0 ? "FALSE" : "TRUE");
                OldConnectionString = conn;
                using (SqlCeConnection localConnection = new SqlCeConnection(conn))
                {
                    using (SqlCeEngine engine = new SqlCeEngine(conn))
                    {
                        string newConn = string.Format("Data Source={0};Password={1};Max Database Size=4091;Max Buffer Size = 1024;Default Lock Escalation =100;Encrypt Database={2}", txtFile.Text, txtNewPassword.Text, txtNewPassword.Text.Length == 0 ? "FALSE" : "TRUE");
                        engine.Compact(newConn);
                        NewConnectionString = newConn;
                    }
                    MessageBox.Show("Password has been changed successfully.");                                        
                }
                btnHiddenOK.PerformClick();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                NewConnectionString = string.Empty;
                OldConnectionString = string.Empty;
            }
        }      

        private void btnSelect_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = "Open the SQL CE File";
            openFileDialog.Filter = "SQL CE Files (*.sdf)|*.sdf";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFile.Text = openFileDialog.FileName;
                txtPassword.Focus();
            }
        }

    }
}